package com.asset.controller;

import com.asset.common.BaseResponse;
import com.asset.common.ErrorCode;
import com.asset.common.ResultUtils;
import com.asset.model.entity.DepreciationDetail;
import com.asset.model.entity.DepreciationRecord;
import com.asset.service.DepreciationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.asset.common.ErrorCode.SYSTEM_ERROR;

/**
 * 折旧控制层
 */
@RestController
@RequestMapping("/depreciation")
public class  DepreciationController {
    @Autowired
    private DepreciationService depreciationService;

    /**
     * 定时任务，定期执行折旧，存放到折旧记录表
     * @return
     */
    @GetMapping("updateEveryMonth")
    public BaseResponse<String> updateEveryMonth() {
        depreciationService.updateEveryMonth();

        return ResultUtils.success("操作成功");
    }

    /**
     * 根据资产id返回对应的折旧信息
     */
    @GetMapping("/getById")
    public BaseResponse<List<DepreciationRecord>> getById(Integer id) {
        List<DepreciationRecord> getRecordByAssetId = depreciationService.getRecordByAssetId(id);
        return ResultUtils.success(getRecordByAssetId);
    }

    /**
     * 返回所有本公司可以查看的折旧记录(不包括系统自动折旧的)
     */
    @GetMapping("/getRecordByCompanyId")
    public BaseResponse<List<DepreciationRecord>> getRecordByCompanyId(Integer companyId) {
        List<DepreciationRecord> getRecordByCompanyId = depreciationService.getRecordByCompanyId(companyId);


        return ResultUtils.success(getRecordByCompanyId);
    }

    /**
     * 返回所有本公司每月系统自动折旧的记录
     */
    @GetMapping("/getRecordByCompanyIdSys")
    public BaseResponse<List<DepreciationRecord>> getRecordByCompanyIdSys(Integer companyId) {
        List<DepreciationRecord> getRecordByCompanyId = depreciationService.getRecordByCompanyIdSys(companyId);


        return ResultUtils.success(getRecordByCompanyId);
    }

    /**
     * 根据折旧id返回折旧信息表
     */
    @GetMapping("/getDetailByRecordId")
    public BaseResponse<List<DepreciationDetail>> getDetailByRecordId(Integer recordId) {
        List<DepreciationDetail> getDetailByRecordId = depreciationService.getDetailByRecordId(recordId);


        return ResultUtils.success(getDetailByRecordId);
    }

    /**
     * 获取资产折旧信息进行更新
     */
    @PostMapping("/update")
    public BaseResponse<String> update(@RequestBody DepreciationDetail depreciationDetail) {
        //获取资产明细的折旧信息
        try {
            depreciationService.update(depreciationDetail);

        }catch (Exception e){
            return ResultUtils.error(SYSTEM_ERROR);
        }
        return ResultUtils.success("操作成功");
    }

    /**
     * 删除资产折旧信息,删除折旧记录表（逻辑删除），折旧记录看不到了，但是实际任然存在在数据库中
     */
    @GetMapping("/delete")
    public BaseResponse<String> delete(Integer recordId) {
        depreciationService.delete(recordId);
        return ResultUtils.success("操作成功");
    }

    /**
     * 点击确定按钮时，更新折旧记录信息
     */
    @GetMapping("/updateRecord")
    public BaseResponse<String> updateRecord(Integer recordId) {
        depreciationService.updateRecord(recordId);
        return ResultUtils.success("操作成功");
    }
}
